package com.stoken.web.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.stoken.dao.TravelApplyDao;
import com.stoken.domain.LineUse;
import com.stoken.domain.MemberBaseInfo;
import com.stoken.domain.MemberShopWallet;
import com.stoken.domain.Notice;
import com.stoken.domain.TravelApply;
import com.stoken.domain.TypeLineInfo;
import com.stoken.service.LineUseService;
import com.stoken.service.MemberShopWalletService;
import com.stoken.service.TravelApplyService;
import com.stoken.service.TypeLineInfoService;
import com.stoken.web.StokenUtils;
import com.stoken.web.control.vo.Message;
import com.symbio.commons.Compositor;
import com.symbio.commons.Compositor.CompositorType;
import com.symbio.commons.Filtration;
import com.symbio.commons.Page;
import com.symbio.utils.WebUtils;

@Controller
@RequestMapping("/travelApply")
public class TravelApplyControl {
	
	@Autowired
	private TravelApplyService travelApplyService;
	
	@Autowired
	private TypeLineInfoService typeLineInfoService;
	
	
	
	/**
	 * 申请路线界面
	 */
	@RequestMapping(value ="/index")
	public String index(Notice notice,HttpServletRequest request,ModelMap modeMape,String tId){			
		TypeLineInfo lineInfo = typeLineInfoService.get(new Long(tId));
		request.setAttribute("lineInfo", lineInfo);
		return "/travel/signup";
	}
	
	@RequestMapping(value ="/apply")
	public void apply(HttpServletRequest request,HttpServletResponse response,String tId,String applyTime,TravelApply apply){
		String[] parsePatterns = new String[]{"yyyy-MM-dd"};
		Date date = new Date();
		try {
			 date = DateUtils.parseDate(applyTime, parsePatterns );
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		TypeLineInfo info = new TypeLineInfo();
		info.setId(new Long(tId));
		apply.setBaseUsername(mbi);
		apply.setUsername(mbi.getBaseUsername());
		apply.setLineInfo(info);
		apply.setApplyDate(date);
		apply.setCreateTime(new Date());
		travelApplyService.save(apply);
		print(response,new Message(1,""));
	}
	
	/**
	 * 输出
	 * @param response
	 * @param message
	 */
	private void print(HttpServletResponse response,Message message){
		try {
			String json = "{\"code\":\""+message.getCode()+"\",\"context\":\""+message.getContext()+"\"}";  
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(json);
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@RequestMapping(value ="/recodeIndex")
	public String recodeIndex(HttpServletRequest request){
		return "travel/travelRecordList";
	}
	
	@RequestMapping(value ="/recodePage")
	public String recodePage(HttpServletRequest request){
		MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<TravelApply> pageData = new Page<TravelApply>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("createTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
		List<Filtration> filtrations = new ArrayList<Filtration>();
		filtrations.add(new Filtration("EQS_username", mbi.getBaseUsername()));
		
		
		pageData = travelApplyService.find(pageData);		
		request.setAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		
		request.setAttribute("pagination", page.showPages());
	
		
		return "travel/traveRecordlPage";
	}
	
	@RequestMapping("/deleteRecord")
	public String deleteRecode(HttpServletRequest request,ModelMap modeMape,Long recordId){		
		TravelApply travlApp = travelApplyService.find(recordId);
		if(travlApp != null){
			travelApplyService.delete(travlApp);
		}	
		return "travel/travelRecordList";
	}
	
	/***/
	
	@RequestMapping(value ="/merchant/recodeIndex")
	public String recodeMerchantIndex(HttpServletRequest request){
		return "merchant/travel/travelRecordList";
	}
	
	@RequestMapping(value ="/merchant/recodePage")
	public String recodeMerchantPage(HttpServletRequest request){
		//MemberBaseInfo mbi = (MemberBaseInfo) request.getSession().getAttribute(StokenUtils.LOGINACCOUNT);
		Page<TravelApply> pageData = new Page<TravelApply>();
		//给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		Compositor compositor = new Compositor("createTime",CompositorType.DESC);
		pageData.setCompositor(compositor);
		
//		List<Filtration> filtrations = new ArrayList<Filtration>();
//		filtrations.add(new Filtration("EQS_username", mbi.getBaseUsername()));
		
		
		pageData = travelApplyService.find(pageData);		
		request.setAttribute("pageData", pageData);
	    
	    Pagination page = new Pagination();
		page.setIntTotalNum((int)pageData.getPagination().getTotalCount());
		page.setCurpage(pageData.getPagination().getPageNo());
		page.setPagenum(pageData.getPagination().getPageSize());
		request.setAttribute("pagination", page.showPages());
		
		return "merchant/travel/traveRecordlPage";
	}
	
	
	@RequestMapping(value ="/merchant/findRecord")
	public String findRecord(HttpServletRequest request,String recordId,String pageNo){
		TravelApply apply = travelApplyService.get(new Long(recordId));
		request.setAttribute("apply", apply);
		request.setAttribute("pageNo", pageNo);
		return "merchant/travel/signup";
	}
	
	@RequestMapping(value ="/merchant/apply")
	public String merchantApply(HttpServletRequest request,HttpServletResponse response,String recordId){
		TravelApply apply = travelApplyService.get(new Long(recordId));
		String state = request.getParameter("state");
		if(StringUtils.isNotBlank(state))
		{
			apply.setState(new Integer(state));
		}
		
		String pageNo = request.getParameter("pageNo");
		if(StringUtils.isNotBlank(pageNo))
		{
			request.setAttribute("pageNo", pageNo);
		}else
		{
			request.setAttribute("pageNo", 1);
		}
		travelApplyService.update(apply);
		return "merchant/travel/travelRecordList";
	}
}
